package com.module.circle.ui.medalDetails

import android.annotation.SuppressLint
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.common.app.base.AppActivity
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.circle.CirclesMedalModel
import com.common.app.helper.UserHelper
import com.common.app.utls.HeadTitleUtils
import com.common.base.adapter.BaseFragmentPageAdapter
import com.common.base.app.extras.dp2px
import com.common.base.app.extras.getShowMsg
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.showToast
import com.common.base.app.extras.yes
import com.common.base.utils.StatusBarUtils
import com.module.circle.R
import com.module.circle.databinding.CircleActivityMedalDetailsBinding


/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/08/14
 * <p>
 * 描述：勋章详情页面
 * <p>
 * 修订历史：
 */
class MedalDetailsActivity :
    AppActivity<CircleActivityMedalDetailsBinding, MedalDetailsViewModel>(),
    View.OnClickListener {

    @Autowired(name = KeyBundle.BUNDLE_KEY)
    @JvmField
    var mBean: CirclesMedalModel? = null

    val titles = ArrayList<String>()

    override fun getViewModel() = MedalDetailsViewModel::class.java

    override fun getViewContentBinding() = CircleActivityMedalDetailsBinding.inflate(layoutInflater)

    override fun getPageTitle(): String = ""
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    override fun initViews() {
        super.initViews()

        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle("")
        headTitleUtils.setLineStyle(10)

//        mViewContentBinding.viewPager.pageMargin = dp2px(8)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun initEvents() {
        super.initEvents()
//        mViewContentBinding.tvTitleRight.setOnClickListener(onClick = this)

        mViewContentBinding.ly.setOnTouchListener { v, event ->
            mViewContentBinding.viewPager.dispatchTouchEvent(
                event
            )
        }

        mViewContentBinding.viewPager.addOnPageChangeListener(object :
            ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                mBean?.let { beanIt ->
                    if (beanIt.badges.isNullOrEmpty()) return@let
                    setPositionUi(position)
                }
            }
        })

    }

    override fun lazyInitData() {
        super.lazyInitData()
        mBean?.let {
            val mFragments = arrayListOf<Fragment>()
            it.badges?.forEach { badgesIt ->
                titles.add(badgesIt.name ?: "")
                mFragments.add(MedalDetailsFragment.newInstance(badgesIt.name ?: "", badgesIt))
            }

            val mAdapter = BaseFragmentPageAdapter(supportFragmentManager, mFragments, titles)
            mViewContentBinding.viewPager.adapter = mAdapter
            mViewContentBinding.viewPager.offscreenPageLimit = mFragments.size
//                        mAdapter.setNoDestroy(0, 1)
            mViewContentBinding.viewPager.currentItem = 0
            setPositionUi(0)
        }
    }

    override fun onClick(v: View?) {
        when (v) {
//            mViewContentBinding.tvTitleRight -> LaunchHelper.launchWebActivity(ApiConstant.SECKILL_RULE)
        }
    }

    fun setPositionUi(position:Int){
        mBean?.let { beanIt ->
            if (beanIt.badges.isNullOrEmpty()) return@let
            beanIt.badges!![position].let {
                mViewContentBinding.tvName.text = it.name
                mViewContentBinding.tvDes.text = it.description
                mViewContentBinding.sbrProgress.setMaxProgress(it.require)
                mViewContentBinding.sbrProgress.setProgress(beanIt.progress)
                mViewContentBinding.tvProgress.text =
                    "${beanIt.progress}/${it.require}${beanIt.unit}"
            }
        }
    }

    private fun getMedalList() {
//        showDialogLoading()
//        mViewModel.getMedalList(UserHelper.getUser()?.user_id?:0) {
//            showToast(it.getShowMsg())
//        }.observe(this) { rsoIt ->
//            dismissLoadingDialog()
//            rsoIt.isSuccess().yes {
//                rsoIt.Data?.let { dataIt ->
//                    val listTemp = dataIt.filter { it.id == mId }
//                    listTemp[0].let { mBeanIt->
//                        val mFragments = arrayListOf<Fragment>()
//                        mBeanIt.badges?.forEach {badgesIt->
//                            titles.add(badgesIt.name?:"")
//                            mFragments.add(MedalDetailsFragment.newInstance(badgesIt.name?:"",badgesIt))
//                        }
//
//                        val mAdapter = BaseFragmentPageAdapter(supportFragmentManager, mFragments, titles)
//                        mViewContentBinding.viewPager.adapter = mAdapter
//                        mViewContentBinding.viewPager.offscreenPageLimit = mFragments.size
////                        mAdapter.setNoDestroy(0, 1)
//                    }
//                }
//            }.otherwise {
//                showToast(rsoIt.Message)
//            }
//        }
    }
}